package logic

import (
	"context"
	"fmt"
	"house123/C/srv/model/mysql"
	"house123/C/srv/model/redis"

	"house123/C/srv/house"
	"house123/C/srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *house.LoginRequest) (*house.LoginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get(context.Background(), "Sms:login"+in.Username)
	if get.Val() != in.Send {
		logx.Error("验证码错误")
		return nil, fmt.Errorf("验证码错误")
	}
	var u mysql.User
	err := u.Find(in.Username)
	if err != nil {
		logx.Error("没找到给用户")
		return nil, fmt.Errorf("没找到给用户")
	}
	if u.Password != in.Password {
		logx.Error("密码错误")
		return nil, fmt.Errorf("密码错误")
	}
	redis.Client.Del(context.Background(), "Sms:login"+in.Username)
	return &house.LoginResponse{UserId: int64(u.ID)}, nil
}
